package com.ruoyi.web.controller.module;

import java.util.List;
import java.util.Arrays;

import cn.hutool.json.JSONUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.MapArea;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.module.IMapAreaService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 地图区域管理Controller
 *
 * @author ruoyi
 * @date 2022-10-18
 */
@Validated
@Api(value = "地图区域管理控制器", tags = {"地图区域管理管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/map/area")
public class MapAreaController extends BaseController {

    private final IMapAreaService mapAreaService;

    /**
     * 查询地图区域管理列表
     */
    @ApiOperation("查询地图区域管理列表")
    @PreAuthorize("@ss.hasPermi('module:mapArea:list')")
    @GetMapping("/list")
    public TableDataInfo<MapArea> list(@Validated MapArea mapArea) {
        QueryWrapper<MapArea> qw = new QueryWrapper<>();

        TableDataInfo<MapArea> td = PageUtils.buildDataInfo(mapAreaService.page(PageUtils.buildPage(),qw));
        List<MapArea> list = td.getRows();
        return td;
    }

    /**
     * 导出地图区域管理列表
     */
    @ApiOperation("导出地图区域管理列表")
    @PreAuthorize("@ss.hasPermi('module:mapArea:export')")
    @Log(title = "地图区域管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<MapArea> export(@Validated MapArea mapArea) {
        List<MapArea> list = mapAreaService.list();
        ExcelUtil<MapArea> util = new ExcelUtil<MapArea>(MapArea.class);
        return util.exportExcel(list, "地图区域管理");
    }

    /**
     * 获取地图区域管理详细信息
     */
    @ApiOperation("获取地图区域管理详细信息")
    @PreAuthorize("@ss.hasPermi('module:mapArea:query')")
    @GetMapping("/{id}")
    public AjaxResult<MapArea> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        MapArea mapArea = mapAreaService.getById(id);
        return AjaxResult.success(mapArea);
    }

    /**
     * 新增地图区域管理
     */
    @ApiOperation("新增地图区域管理")
    @PreAuthorize("@ss.hasPermi('module:mapArea:add')")
    @Log(title = "地图区域管理", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody MapArea mapArea) {
    	if(mapArea.getChoosePoints() != null){
    		String points = JSONUtil.toJsonStr(mapArea.getChoosePoints());
			mapArea.setPoints(points);
		}
        return toAjax(mapAreaService.save(mapArea) ? 1 : 0);
    }

    /**
     * 修改地图区域管理
     */
    @ApiOperation("修改地图区域管理")
    @PreAuthorize("@ss.hasPermi('module:mapArea:edit')")
    @Log(title = "地图区域管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody MapArea mapArea) {
		if(mapArea.getChoosePoints()!=null){
			String points = JSONUtil.toJsonStr(mapArea.getChoosePoints());
			mapArea.setPoints(points);
		}
        return toAjax(mapAreaService.updateById(mapArea) ? 1 : 0);
    }

    /**
     * 删除地图区域管理
     */
    @ApiOperation("删除地图区域管理")
    @PreAuthorize("@ss.hasPermi('module:mapArea:remove')")
    @Log(title = "地图区域管理" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(mapAreaService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
